/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package rs232_test;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.SwingWorker;

/**
 *
 * @author sf
 */
public class MainClass extends SwingWorker<String, Object> {

    static Thread readThread;
    public String stringUpdate;
    public Rs485.RxCommPack rxCommPack;
    static public NewJFrame njf;
    static private Rs485.RxCommPack evtRxCommPack;

    @Override
    public String doInBackground() {
        //rs_test reader = new rs_test();
        //reader.run();
        return "test";
    }

    @Override
    protected void done() {
        try {
            System.out.println("arrivati al done");
        } catch (Exception ignore) {
        }
    }

    public static void main(String[] args) {

        // readThread = new Thread(reader);
        //readThread.start();

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                njf = new NewJFrame();
                njf.setVisible(true);
            }
        });

        MainClass worker = new MainClass();

        //rs_test obj = new rs_test();
        worker.addPropertyChangeListener(
                new PropertyChangeListener() {

                    public void propertyChange(PropertyChangeEvent evt) {
                        if ("stringupdate".equals(evt.getPropertyName())) {
                            System.out.println((String) evt.getNewValue());
                            njf.jDataPayload.setText((String) evt.getNewValue());
                        }
                    }
                });

        worker.addPropertyChangeListener(
                new PropertyChangeListener() {

                    public void propertyChange(PropertyChangeEvent evt) {
                        if ("rxcommupdate".equals(evt.getPropertyName())) {
                             evtRxCommPack = (Rs485.RxCommPack) evt.getNewValue();
                          //  njf.jDataPayload.setText((String) evt.getNewValue());

                             byte[] a = new byte[1];
                             a[0] = evtRxCommPack.cCommand;
                             String cmd = new String(a);
                             njf.jTextCmd.setText(cmd);
                        }
                    }
                });
        rs_test reader = new rs_test(worker);
    }

    void setStringUpdate(String stringUpdate) {

        if (stringUpdate == this.stringUpdate) {
            return;
        }
        String oldstringUpdate = this.stringUpdate;
        this.stringUpdate = stringUpdate;
        if (getPropertyChangeSupport().hasListeners("stringupdate")) {
            firePropertyChange("stringupdate", oldstringUpdate, this.stringUpdate);
        }
    }

    void setRxPackUpdate(Rs485.RxCommPack rxCommPack) {

        if (rxCommPack == this.rxCommPack) {
            return;
        }
        Rs485.RxCommPack oldrxCommPack = this.rxCommPack;
        this.rxCommPack = rxCommPack;
        if (getPropertyChangeSupport().hasListeners("rxcommupdate")) {
            firePropertyChange("rxcommupdate", oldrxCommPack, this.rxCommPack);
        }
    }

    public Rs485.RxCommPack getRxPackUpdate() {
        return rxCommPack;
    }

    public String getStringUpdate() {
        return stringUpdate;
    }
}

